package com.admin.service.impl;

import com.admin.entity.Station;
import com.admin.entity.Terminal;
import com.admin.mapper.AdminMapper;
import com.admin.mapper.StationMapper;
import com.admin.mapper.TerminalMapper;
import com.admin.service.ITongjiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@Service
public class TongjiserviceImpl implements ITongjiService {
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TerminalMapper terminalMapper;
    /**
     * 根据用户id查询统计终端数据
     * @param id 当前用户id
     * @return 在线总时长，绑定终端数，在线数，离线数
     *"zhongduanzongtime","zhongduanshu","zaixianshu","lixianshu"
     */
    @Override
    public Map tongJizhongduan(int id) {
        Map map = new HashMap();
        int zhongduanshu = 0;
        int zhongduanzongtime = 0;
        int zaixianshu = 0;
        int lixianshu = 0;
        String admins = adminMapper.getAllChildUser(id);
        List<Station> stations = stationMapper.getSuoyouzhandianbyid(admins);
        if(stations!=null){
            for(Station s : stations){
                if(s.getStaionCurrentNumberOTerminals()!=null){
                    zhongduanshu+=s.getStaionCurrentNumberOTerminals();
                }
            }
            List<Terminal> terminals = terminalMapper.tongJizhongduan(stations);
            if (terminals!=null){
                for (Terminal t : terminals){
                    if(t.getTerminalTotalOnlineTime()!=0){
                        zhongduanzongtime+=t.getTerminalTotalOnlineTime();
                    }
                    if("离线".equals(t.getTerminalLine())){
                        lixianshu++;
                    }
                }
                zaixianshu = zhongduanshu-lixianshu;
            }
        }
        map.put("zhongduanshu",zhongduanshu);
        map.put("zhongduanzongtime",zhongduanzongtime);
        map.put("zaixianshu",zaixianshu);
        map.put("lixianshu",lixianshu);
        return map;
    }
}
